package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.ApnSpeedLimitAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.util.ApnSpeedLimitUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@AttrHandler(ApnSpeedLimitAttrHandler.class)
public class ApnSpeedLimitAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 取所有接入类产品
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(data.getSceneInstId());
        final KiteProdInst[] prodInsts = new KiteProdInst[1];
        if (KiteListUtils.isNotEmpty(kiteProdInstList)) {
            kiteProdInstList.stream().forEach(prodInst -> {
                List<KiteProdInstFunc> kiteProdInstFuncs = querySceneInstService
                    .listProdInstFuncs(data.getCustId(), prodInst.getProdInstId());
                if (KiteListUtils.isNotEmpty(kiteProdInstFuncs)) {
                    kiteProdInstFuncs.stream().forEach(kiteProdInstFunc -> {
                        //取资费计划
                        if (KiteStringUtils.equals(ProductCache.get(kiteProdInstFunc.getSpecId()).getProdNbr(), LocalKeyConsts.FUNC_PROD_ID_ZFFW)) {
                            prodInsts[0] = prodInst;
                            return;
                        }
                    });
                }
            });
        }
        if (!KiteObjectUtils.isEmpty(prodInsts) && !KiteObjectUtils.isEmpty(prodInsts[0])) {
            //取资费计划的功能产品
            List<KiteProdInstFunc> kiteProdInstFuncs = querySceneInstService.listProdInstFuncs(data.getCustId(), prodInsts[0].getProdInstId());
            String apnSpeedLimitValue = attr.getValue();
            if (Objects.nonNull(kiteProdInstFuncs) && kiteProdInstFuncs.size() > 0) {
                kiteProdInstFuncs.forEach(kiteProdInstFunc -> {
                    Map<String, KiteAttr> kiteAttrMap = kiteProdInstFunc.getAttrMap();
                    if (Objects.nonNull(kiteAttrMap)) {
                        Iterator<Map.Entry<String, KiteAttr>> it = kiteAttrMap.entrySet().iterator();
                        while (it.hasNext()) {
                            Map.Entry<String, KiteAttr> entry = it.next();
                            ApnSpeedLimitUtil.linkFeePlanAttr(entry.getValue(), kiteProdInstFunc, data.getSceneInstId(), apnSpeedLimitValue);
                        }
                    }
                });
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
